package server.service.impl;

import com.alibaba.fastjson.JSON;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import server.config.exception.BizException;
import server.dao.BlanceRecordDao;
import server.dao.FrmDictionDao;
import server.dao.UserBlanceDao;
import server.dao.UsersDao;
import server.model.dto.TransDto;
import server.model.entity.*;
import server.service.UserBlanceService;
import server.tool.tool.BeanUtil;

import javax.transaction.Transactional;
import java.math.BigDecimal;
import java.util.Optional;

/**
 * 创建人:    xvzhe
 * 创建时间:
 * 修改时间:
 * 版本:      v1.0
 */
@Service
public class UserBlanceServiceImpl implements UserBlanceService {
    @Autowired
    UserBlanceDao userBlanceDao;
    @Autowired
    BlanceRecordDao blanceRecordDao;
    @Autowired
    FrmDictionDao frmDictionDao;
    @Autowired
    UsersDao usersDao;

    @Override
    public Boolean createBlance(Users us) {
        UserBlance.Type[] va = UserBlance.Type.values();
        for (int i = 0; i < va.length; i++) {
            UserBlance userBlance = new UserBlance();
            userBlance.setType(va[i]);
            userBlance.setMoney(BigDecimal.ZERO);
            userBlance.setFrostMoney(BigDecimal.ZERO);
            userBlance.setUserId(us.getId());
            userBlanceDao.save(userBlance);
        }
        return true;
    }

    @Transactional
    @Override
    public Boolean insBlance(String userId, BigDecimal money, UserBlance.Type type, BlanceRecord.Type bType, String... log) {
        UserBlance userBlance = userBlanceDao.findByTypeAndUserId(type, userId);
        BlanceRecord blanceRecord = new BlanceRecord();
        if (bType.equals(BlanceRecord.Type.TOTAL_INS)) {
            money = money.abs();
            blanceRecord.setLog("签到" + type.getDisplay());
        }
        if (bType.equals(BlanceRecord.Type.TOTAL_EEL)) {
            money = money.abs().negate();
            blanceRecord.setLog("抽奖扣除" + type.getDisplay());
        }
        if (bType.equals(BlanceRecord.Type.TOTAL_III)) {
            money = money.abs();
            blanceRecord.setLog("抽奖奖励" + type.getDisplay());
        }
        if (bType.equals(BlanceRecord.Type.TRANS_DEL)) {
            money = money.abs().negate();
            blanceRecord.setLog("余额转出" + type.getDisplay());
        }
        if (bType.equals(BlanceRecord.Type.TRANS_INS)) {
            money = money.abs();
            blanceRecord.setLog("余额转入" + type.getDisplay());
        }
        if (bType.equals(BlanceRecord.Type.DEPOSIT)) {
            money = money.abs().negate();
            blanceRecord.setLog("提现" + type.getDisplay());
        }
        if (bType.equals(BlanceRecord.Type.AD_INS)) {
            money = money.abs();
            blanceRecord.setLog("充值" + type.getDisplay());
        }
        if (bType.equals(BlanceRecord.Type.AD_DEL)) {
            money = money.abs().negate();
            blanceRecord.setLog("扣减" + type.getDisplay());
        }
        blanceRecord.setStatus(true);
        blanceRecord.setType(bType);
        blanceRecord.setUType(type);
        blanceRecord.setBlanceId(userBlance.getId());
        blanceRecord.setUserId(userId);
        if (log.length > 0) {
            blanceRecord.setLog(log[0]);
        }
        blanceRecord.setAfter(userBlance.getMoney().add(money));
        blanceRecord.setBefor(userBlance.getMoney());
        blanceRecordDao.save(blanceRecord);
        if (userBlance.getMoney().add(money).compareTo(BigDecimal.ZERO) < 0) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new BizException("余额不足");
        }
        userBlance.setMoney(userBlance.getMoney().add(money));
        userBlanceDao.save(userBlance);
        return true;
    }

    @Override
    @Transactional
    public Boolean trans(String userId, TransDto transDto) {
        UserBlance userBlance = userBlanceDao.findByTypeAndUserId(UserBlance.Type.RMB, userId);
        /*FrmDiction fm = frmDictionDao.findByKeyst("HZ");*/
/*
        BigDecimal sxf = new BigDecimal(fm.getVal()).multiply(transDto.getMoney());
*/
        if (userBlance.getMoney().subtract(transDto.getMoney()).compareTo(BigDecimal.ZERO) < 0) {
            throw new BizException("余额不足");
        }
        Users tuser = usersDao.findByMobile(transDto.getMobile());
        if (!Optional.ofNullable(tuser).isPresent()) {
            throw new BizException("手机号码不存在！");
        }
        UserBlance trUser = userBlanceDao.findByTypeAndUserId(UserBlance.Type.RMB, tuser.getId());
        if (Optional.ofNullable(trUser).isPresent()) {
            insBlance(userId, transDto.getMoney(), UserBlance.Type.RMB, BlanceRecord.Type.TRANS_DEL);
            insBlance(trUser.getUserId(), transDto.getMoney(), UserBlance.Type.RMB, BlanceRecord.Type.TRANS_INS);
        } else {
            throw new BizException("对方钱包不存在");
        }
        return true;
    }
}
